<?php
/**
 * Smarty plugin
 *
 * @package Smarty
 * @subpackage PluginsFunction
 */

/**
 * Smarty {html_url} function plugin
 *
 * Type:     function<br>
 * Name:     html_url<br>
 * Date:     October 19, 2012
 * Purpose:  automate url address link creation, and optionally encode them.<br>
 * Params:
 * <pre>
 * - page    - (optional) - url address
 * - text    - (optional) - text to display, default is url address
 * - target	 - (optional) - target attribute
 * - reqkeys    - (required) - _REQUEST keys name
 * </pre>
 * Examples:
 * <pre>
 * {{html_url c=good a=list reqkey='cid,bid,cvrank,pricerank'}}
 * {{html_url page="pro.php" c=good a=list}}
 * </pre>
 *
 * @link http://www.ldyy8.com
 * @version 1.0
 * @author ZhangXiLian 490292@qq.com
 * @param array              $params   parameters
 * @return string
 */
function smarty_function_html_url($params, $template)
{
	static $_fixed_attribute = array('page','reqkeys');
	
	$target = $params['target'];
	if(!empty($target)){
		$target = ' target="'.$target.'"';
	}
	
	$url = "";
	$params_keys = array_keys($params);
	if (count($params_keys) > 0){
		foreach ($params_keys as $value)
        {
        	if(!in_array($value,$_fixed_attribute))
        		$url .= $value . "=" . $params[$value] . "&";
        }        
	}
	if(!empty($params["reqkeys"])){		
		$keyArr = explode(",",$params["reqkeys"]);
		if (count($keyArr) > 0){
			$reqsData = $_REQUEST;
			foreach ($keyArr as $value)
	        {
	        	$url .= $value . "=" . $reqsData[$value] . "&";
	        }
		}		
	}
	if(substr($url, -1)==="&"){
		$url = substr($url, 0, strlen($url)-1);
	}
	
	return $params['page']."?".$url;
}

?>